import type { AssignDuplicate, AssignInfo, AssignReport } from '@/types/assign'
import http from './http'
import { apiMsgProp } from '@/utils/global'

const AssignService = {
    /**
     * @function getById 通过反思报告id获取反思报告
     * @param id 反思报告 id
     * @returns AssignInfo 反思报告信息
     */
    getById: async (id: number): Promise<AssignInfo> => {
        const res = await http.get(`/course/reflection/getReflectionInfo/${id}`)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data.data
    },
    /**
     * @function getByCourseId 通过课程id获取课程反思报告
     * @param id 课程 id
     * @returns AssignInfo 反思报告信息
     */
    getByCourseId: async (id: number): Promise<AssignInfo> => {
        const res = await http.get(`/course/reflection/course/${id}`)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data.data
    },
    /**
     * @function save 保存课程反思报告
     * @param data 反思报告信息
     */
    save: async (data: AssignInfo): Promise<void> => {
        const res = await http.post(`/course/resource/reflection/save`, data)
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)
    },
    /**
     * @function list 获取课程反思报告列表
     * @param id 课程 id
     * @returns AssignInfo[] 反思报告信息
     */
    list: async (id: number, form: any): Promise<{ list: AssignReport[]; limit: number; page: number; total: number; type: number }> => {
        const { page, limit, first_initial, last_initial, type, no_page } = form
        let url = `/course/reflection/course/${id}/list`
        if (no_page) url += `?no_page=${no_page}`
        if (page) url += `?page=${page}`
        if (limit) url += `&limit=${limit}`
        if (first_initial && first_initial !== 'all') url += `&first_initial=${first_initial}`
        if (last_initial && last_initial !== 'all') url += `&last_initial=${last_initial}`
        if (type) url += `&type=${type}`

        const res = await http.get(url)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data.data
    },
    /**
     * @function saveToStudent 学生上传反思报告
     * @param reflection_id 反思报告 id
     * @param file_url 上传的文件 url
     * @returns
     */
    saveToStudent: async (reflection_id: number, file: File): Promise<void> => {
        const formData = new FormData()
        formData.append('file', file)
        formData.append('reflection_id', reflection_id.toString())
        const res = await http.post(`/course/reflection/save`, formData, {
            headers: {
                'Content-Type': 'multipart/form-data',
            },
            timeout: undefined,
        })
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)
    },
    /**
     * @function submitToStudent 学生提交反思报告
     * @param reflection_id 反思报告 id
     * @returns
     */
    submitToStudent: async (reflection_id: number): Promise<void> => {
        const res = await http.get(`/course/reflection/submit?reflection_id=${reflection_id}`)
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)
    },
    /**
     * @function delToStudent 学生删除已上传的反思报告
     * @param reflection_id 反思报告 id
     * @returns
     */
    delToStudent: async (reflection_id: number): Promise<void> => {
        const res = await http.get(`/course/reflection/remove?reflection_id=${reflection_id}`)
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)
    },
    /**
     * @function downloads 批量下载反思报告
     * @param courseId 课程 id
     * @returns
     */
    downloads: async (courseId: number, form: any): Promise<any> => {
        const { first_initial, last_initial, type } = form
        let url = `/course/reflection/course/${courseId}/export?type=${type ? type : ''}&first_initial=${first_initial && first_initial !== 'all' ? first_initial : ''}&last_initial=${last_initial && last_initial !== 'all' ? last_initial : ''}`
        const res = await http.get(url)
        if (res.status !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data
    },
    /**
     * @function batchDelaySubmit 批量延期提交
     * @param ids 学生反思报告成绩列表 id
     * @param delay_days 延期时间 2025-12-21 12:32:22
     * @returns
     */
    delaySubmitToStudents: async (reflect_id: number, student_id: number[], delay_days: string): Promise<void> => {
        const res = await http.post(`/course/reflection/batch-delay-submit`, { student_id, delay_days, reflect_id })
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)
    },
    /**
     * @function getByStudentId 获取学生反思报告
     * @param id 课程id
     * @param studentId 学生id
     * @returns
     */
    getByStudentId: async (id: number, studentId: number): Promise<AssignInfo> => {
        const res = await http.get(`/course/reflection/course/${id}/student/${studentId}`)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data.data
    },
    /**
     * @function score 进行评分
     * @param data 最终评语 + 每个标准的评语 + 反思报告记录(submission) id
     * @returns
     */
    score: async (data: { id: number; teacher_comment: string; score_result: any }): Promise<void> => {
        const res = await http.post(`/course/reflection/score`, data)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
    },
    /**
     * @function backDraftToStudents 将作業回復為草稿
     * @param ids 学生反思报告成绩列表 id
     * @returns
     */
    backDraftToStudents: async (reflect_id: number, student_id: number[]): Promise<void> => {
        const res = await http.post(`/course/reflection/batch-back-draft`, { student_id, reflect_id })
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)
    },
    /**
     * @function refresh 获取反思报告的查重结果
     * @param reflection_id 反思报告 id
     * @param student_id 学生 id
     * @returns AssignDuplicate 反思报告查重结果
     */
    refresh: async (reflection_id: number, student_id: number): Promise<AssignDuplicate> => {
        const res = await http.get(`/course/reflection/getReport?reflection_id=${reflection_id}&student_id=${student_id}`)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data.data
    },
    /**
     * @function refresh 获取反思报告的查重结果
     * @param reflection_id 反思报告 id
     * @param student_id 学生 id
     * @param score 分数
     * @param teacher_comment 评语
     * @returns AssignDuplicate 反思报告查重结果
     */
    updateByScore: async (reflection_id: number, student_id: number, score: number, teacher_comment: string): Promise<void> => {
        const res = await http.get(`/course/reflection/editScore?reflect_id=${reflection_id}&student_id=${student_id}&score=${score}&teacher_comment=${teacher_comment}`)
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)
    },
}

export default AssignService
